/**
 * Created by mahai on 2019/1/16.
 * 关于接口
 */

const express = require('express');
const router = express.Router();
const passport = require('passport');
const About = require('../../models/About');


router.get('/',(req,res)=>{
    res.send (`
        status:0 失败 1 不存在 2 成功
        /**
         * [post] /add
         * 新增关于
         * @access 私有接口
         * @param {
         *      userId:用户id,
         *      contact:{}, 联系方式   必填
         *      dec:自我描述，必填
         *      extra_params：{} 扩展字段
         *      WellknownSaying:{}  名人名言 必填
         *      img：头像
         *  } 
         */
        /**
         * [POST] api/about/edit/:id
         * @access 私有接口
         * status:0 失败 1 不存在 2 成功
         * {
         *  contact:{} 字段全部更新    必填
         *  dec:自我介绍 必填
         *  img：头像
         *  WellknownSaying:{} 名人名言 必填
         *  extra_params:{}
         * }
         * */
        /**
         * [get] /find/:id
         * @access 公开接口
         */
         /***
         * [get] api/about/user/:id
         * id:用户的id
         * 获取用户有没有关于的数据
         * */
    `);
});

/**
 * [POST] api/about/add
 * @access 私有接口
 * status:0 失败 1 不存在 2 成功
 * */
router.post('/add',passport.authenticate('jwt',{session:false}),(req,res)=>{
    const aboutFiles = {};
    if(req.body.userId) aboutFiles.userId = req.body.userId;
    if(req.body.contact) aboutFiles.contact = req.body.contact;
    if(req.body.dec) aboutFiles.dec = req.body.dec;
    if(req.body.img) aboutFiles.img = req.body.img;
    else aboutFiles.img = "";
    if(req.body.WellknownSaying) aboutFiles.WellknownSaying = req.body.WellknownSaying;
    if(req.body.extra_params){
        let extra_params = req.body.extra_params;
        if(JSON.stringify(extra_params) === '{}'){
            aboutFiles.extra_param = {"exports": "extra_params是扩展字段"};
        } else {
            aboutFiles.extra_params = req.body.extra_params
        }
    }
    //查询是否存
    About.findOne({userId:req.body.userId}).then(about=>{
        if(about){
            res.json({
                msg:'已存在！',
                status:1
            })
        } else {
            new About(aboutFiles).save().then(newAbout => {
                res.json({
                    msg:'新增成功！',
                    status:2,
                    data:newAbout
                })
            }).catch(err=>res.status(400).json({
                status:0,
                msg:err
            }))
        }
    })
});
/**
 * [POST] api/about/edit/:id
 * @access 私有接口
 * status:0 失败 1 不存在 2 成功
 * {
 * contact:[] 字段全部更新    必填
 * dec:自我介绍 必填
 * img：头像
 * WellknownSaying:[] 名人名言 必填
 * extra_params:{}
 * }
 * */
router.post('/edit/:id',passport.authenticate('jwt',{session:false}),(req,res)=>{
    const aboutFiles = {
        update_time:Date.now()
    };
    if(req.body.contact) aboutFiles.contact = req.body.contact;
    if(req.body.dec) aboutFiles.dec = req.body.dec;
    if(req.body.WellknownSaying) aboutFiles.WellknownSaying = req.body.WellknownSaying;
    if(req.body.img) aboutFiles.img = req.body.img;
    if(req.body.img == ''){
        aboutFiles.img = '';
    }
    if(req.body.extra_params){
        let extra_params = req.body.extra_params;
        if(JSON.stringify(extra_params) === '{}'){
            aboutFiles.extra_param = {"exports": "extra_params是扩展字段"};
        } else {
            aboutFiles.extra_params = req.body.extra_params
        }
    }
    About.findOne({_id:req.params.id}).then(about=>{
        if(!about){
            res.json({
                msg:'数据不存在',
                status:1
            });
        }
        About.findOneAndUpdate(
            {_id:req.params.id},
            {$set:aboutFiles},
            {new:true}
        ).then(profile => res.json({
            data:profile,
            msg:'更新成功',
            status:2
        })).catch(err=>res.status(404).json({
            msg:'更新失败',
            status:1
        }))
    }).catch(err => res.status(404).json({
        data:err,
        status:0
    }))
});

/***
 * [get] api/about/find/:id
 * 获取关于的数据
 * */
router.get('/find/:id',(req,res)=>{
    About.findOne({_id:req.params.id}).then(about=>{
        if(!about){
            res.json({
                msg:'数据不存在',
                status:1
            });
        } else {
            res.json({
                msg:'成功',
                data:about,
                status:2
            });
        }
    }).catch(err => res.status(404).json({
        data:err,
        status:0
    }))
});

/***
 * [get] api/about/user/:id
 * id:用户的id
 * 获取用户有没有关于的数据
 * */
router.get('/user/:id',(req,res)=>{
    About.findOne({userId:req.params.id}).then(about=>{
        if(!about){
            res.json({
                msg:'数据不存在',
                status:1
            });
        } else {
            res.json({
                msg:'存在',
                data:about,
                status:2
            });
        }
    }).catch(err => res.status(404).json({
        data:err,
        status:0
    }))
});

module.exports = router;
